<?php
namespace Core\Traits;
use Core\common\Assist;
trait Template
{
    protected static $data = [];
    /**
     * 发送变量
     * @param $key
     * @param string $value
     */
    public function assign($key,$value='')
    {

        if(is_string($key) && $value){
            self::$data[$key] = $value;
        }else{
            if($key && is_array($key)){
                foreach ($key as $k => $v){
                    self::$data[$k] = $v;
                }
            }
        }
    }

    /**
     * 渲染视图
     * @param string $view
     * @param array $data
     * @param string $dir
     */
    public function display($view = '',$data = [],$dir = '')
    {
        if(empty($view)){
            $do = Assist::gpcVal('do') == 'index'?'index/index':Assist::gpcVal('do');
            $controller = explode("/",$do);
            $do = $controller[0];
            $op = $controller[1];
            $view = $do.'/'.$op;

        }
        echo $this->fetch($view,$data,$dir);
    }

    /**
     * 获取页面数据
     * @param $view
     * @param array $data
     * @param string $dir
     * @return mixed
     */
    public function fetch($view,$data = [],$dir = '')
    {
        global $_W,$_GPC;
        if(self::$data || $data){
            $data = array_merge(self::$data,$data);
            foreach ($data as $k => $v){
                ${$k} = $v;
            }
        }
        if(empty($dir)){
            $module = Assist::globalVal('yWe7.module');
            $dir = MODULE_ROOT.'/'.$module.'/view/';
        }
        ob_start();
        include $this->_template($view,$dir);
        $content = ob_get_contents();
        ob_clean();

        return $content;
    }

    /**
     * 编译模板
     * @param $filename
     * @param $dir
     * @return mixed
     */
    private function _template($filename,$dir)
    {
        list($compile,$source) = self::_findTemFile($filename,$dir);
        if(!is_file($source)) {
            exit("视图: {$dir}{$filename} 不存在");
        }

        $paths   = pathinfo($compile);
        $compile = str_replace($paths['filename'],Assist::uniacid().'_'.$paths['filename'],$compile);
        if (DEVELOPMENT || !is_file($compile) || filemtime($source) > filemtime($compile)){
            template_compile($source, $compile, true);
        }

        return $compile;
    }

    /**
     * 搜寻模板
     * @param $filename
     * @param $dir
     * @return array
     */
    private static function _findTemFile($filename,$dir)
    {
        $template = Assist::globalVal('template');
        $module   = Assist::globalVal('yWe7.module');
        $name     = Assist::globalVal('current_module.name');
        $tplFile  = "{$template}/{$name}/{$module}/{$filename}.tpl.php";
        $compile  = IA_ROOT."/data/tpl/app/{$tplFile}";
        if(defined('IN_SYS')){
            $compile = IA_ROOT."/data/tpl/web/{$tplFile}";
        }

        $source = $dir.$filename;
        if(empty($dir)){
            $defineDir = IA_ROOT.'/addons/'.$name;
            $source = $defineDir."/template/{$filename}";
        }
        $htmlSuffix = Assist::globalVal('yWe7.html_suffix');
        $source = $source.$htmlSuffix;

        return [$compile,$source];
    }
}